package org.ainlolcat.ainscience.datasource;

import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.matrix.Matrix;
import org.ainlolcat.ainscience.tools;
import org.jdom.Element;

import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 05.03.12
 * Time: 0:30
 * To change this template use File | Settings | File Templates.
 */
public class FileDataSource implements DataSource {
    private String lineDelimeterSymbols = " ";
    private boolean tabDelimiter = false;
    private int captionStringCount = 1;
    private int captionStringNum = 0;
    private Matrix matrix;

    public void parseTemplate(Element el) {
        if (el.getChild("lineDilemeters")!=null){
            lineDelimeterSymbols = el.getChildText("lineDilemeters");
        }
        if (el.getChild("useTabLineDilemeters")!=null){
            tabDelimiter = tools.parseBoolean(el.getChildText("useTabLineDilemeters"));
        }
        if (el.getChild("captionStringCount")!=null){
            captionStringCount = tools.parseInteger(el.getChildText("captionStringCount"));
        }
        if (el.getChild("captionStringNum")!=null){
           captionStringNum = tools.parseInteger(el.getChildText("captionStringNum"));
        }
    }

    public void init(Element el) {
        try {
            parseFile(el.getAttributeValue("filename"));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void getColumnCount() {
        matrix.getColumnCount();
    }

    public void getRowCount() {
        matrix.getColumn(0).size();
    }

    public Column getColumn(int index) {
        return matrix.getColumn(index);
    }

    private void parseFile(String filename) throws IOException {
        FileInputStream fis = null;
        try{
            fis = new FileInputStream(filename);
            parseStream(fis);
        }catch(IOException e){
            e.printStackTrace();
            throw e;
        }finally{
            fis.close();
        }
    }

    public void parseStream(InputStream file) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(file));
        Matrix current = new Matrix();
        int rowCount = 0;
        boolean needCaption = (captionStringCount>0);
        while (br.ready()){
            String line = br.readLine();
            //AinCalc for caption/trash at begin
            if (needCaption&&captionStringCount>0){
                if (rowCount==captionStringNum){
                    StringTokenizer st = new StringTokenizer(line, lineDelimeterSymbols);
                    int column = 0;
                    while (st.hasMoreElements()){
                        String token = st.nextToken();
                        current.setCaption(column, token);
                        column++;
                    }
                }
                if (rowCount<captionStringCount){
                    rowCount++;
                    continue;
                }
                else{
                    needCaption = false;
                }
            }
            //parse current line
            StringTokenizer st = new StringTokenizer(line, lineDelimeterSymbols);
            int column = 0;
            while (st.hasMoreElements()){
                String token = st.nextToken();
                current.addData(column, token);
                column++;
            }
            rowCount++;
        }
        matrix = current;
    }

}
